Include Directive in C
The #include
is used to import system defined file or the user-defined file contents to the program.
This helps to import the outside source into our program. This process of importing system-defined and user-defined files to our program is known as File Inclusion.
There are 2 types of files that can be included to our program. They are
1) Header file
The header files will provide us with predefined standard library functions. For example the stdio.h
allows provide us with functions to get the input and display the output to the terminal. Some of the functions are printf()
, scanf()
,cout
, cin
, gets
etc. Inorder to used these function, we need to import it to our file as below.
Example Program
#include <stdio.h>
int main()
{
printf("Welcome to C programming");
return 0;
}
In the above program we have imported stdio.h
and used the printf()
function of stdio.h
to print a welcome message, ???Welcome to C programming???.
2) User-defined file
Suppose, if the user want to create and use some custom function from a file we can use this approach. This approach will help the user by avoiding duplication of function across multiple files.
Create a file process.h with the following code.
int add(int a, int b)
{
return a+b;
}
int subtract(int a, int b)
{
return a-b;
}
int multiply(int a, int b)
{
return a*b;
}
int divide(int a, int b)
{
return a/b;
}
The process.h
has 4 functions add, subtract, multiply & divide to perform respective operation and return the result.
Now include process.h
file to your code as shown in the example below.
#include<stdio.h>
// import process.h user-defined file
#include "process.h"
// main function
int main()
{
// add function defined in process.h
int result = add(100, 200);
printf("Result is %d",result);
return 0;
}